package com.uni.exp5.aspect;


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Author:      Unirithe/Mr.Chao
 * CSDN:        https://blog.csdn.net/Unirithe
 * CreatedTime:  2022/1/20 13:10
 **/
@Component
@Aspect
public class UserServiceLogger {
    private static final Logger log = Logger.getLogger(UserServiceLogger.class);

    @Pointcut("execution(* com.uni.exp5.service.*.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void before(JoinPoint jp){
        log.info(String.format("调用: %s 的 %s 方法，方法参数: %s",
        jp.getTarget(),jp.getSignature().getName(), Arrays.toString(jp.getArgs())));
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void afterReturning(JoinPoint jp, Object result){
        log.info(String.format("调用: %s 的 %s 方法, 方法结果: %s",
        jp.getTarget(), jp.getSignature().getName(), result));
    }
}
